<?php
 
include_once("./configure/configure.php");						//--> global var
include_once("./admin/tools/smarty/libs/Smarty.class.php");		//--> out template
include_once("utils/FileUtils.php");				//--> file utils

include_once("impl/CouponImpl.php");
include_once("impl/CouponAttributeImpl.php");

include_once("impl/CategoryImpl.php");
include_once("impl/MerchantImpl.php");

include_once("includeSmarty.php");

$id 	= $_POST['id'];
$page 	= $_POST['page'];
$order 	= $_POST['order'];

if(empty($id)){
	$location = HOME_URL_HTTP;
	
	header("Location: $location");
	exit;
}


$cacheId = $id . '_' . $page;

/*----- out html -----*/
$SUB_DIR	= FileUtils::getDirByString($id) . '/';

$smarty->compile_dir  	= COMPILE_DIR . '/cat/' . $SUB_DIR;
$smarty->cache_dir  	= CACHE_DIR . '/cat/' . $SUB_DIR;

if(!$smarty->isCached('category.html', $cacheId, $cacheId)){	
	$oCouponImpl 	= new CouponImpl(DB_TAG_COUPON);
	$oCAttributeImpl= new CouponAttributeImpl(DB_TAG_COUPON);
	
	$oCategoryImpl	= new CategoryImpl(DB_TAG_COUPON);
	$oMerchantImpl 	= new MerchantImpl(DB_TAG_COUPON);
	
	include_once("includeTag.php");
	include_once("includeCategories.php");
	
	$oCategory 	= $oCategoryImpl->getById($id);
	if(!is_object($oCategory)){
		$location = HOME_URL_HTTP;
		
		header("Location: $location");
		exit;
	}
	
	$catIdsStr 	= $oCategoryImpl->getLeafIdsStr($oCategory->getId(), true);
		
	$where 		= " WHERE status = 1 AND isRecommend = 1 AND id IN (SELECT couponId FROM coupon_category WHERE categoryId in ($catIdsStr))";	
	$orderStr	= " ORDER BY isRecommend, r_sortValue DESC";
	$oRecommendedCoupons 	= $oCouponImpl->getCustoms($where, $orderStr, $page, 5);
	
	foreach ($oRecommendedCoupons as $oCoupon) {
		$oCouponAttribute = $oCAttributeImpl->getLists($oCoupon->getId());
		$oCoupon->setAttributes($oCouponAttribute);
		
		$oMerchant 	= $oMerchantImpl->getById($oCoupon->getMerchantId());
		
		if($oCoupon->getCreativeType() == 1){
			$oImage = $oCouponImpl->getCouponImage($oCoupon->getId());
			
			if(is_object($oImage)){
				$oCoupon->setCouponImage($oImage);
			}else{
				$oCoupon->setCreativeType(2);
			}
		}
		
		if($oCoupon->getCreativeType() == 2){
			$oLogo = $oMerchantImpl->getLogo($oCoupon->getMerchantId());
			
			if(!is_object($oLogo)){
				$oLogo = new ImageDomain();
				
				$oLogo->setId(0);
				$oLogo->setExtName('gif');				
			}
			
			$oMerchant->setLogo($oLogo);
		}
		
		$oCoupon->setMerchant($oMerchant);
		$oCoupon->setCategory($oCategory);
	}
	
	
	$where 		= " WHERE status = 1 AND id IN (SELECT couponId FROM coupon_category WHERE categoryId in ($catIdsStr))";	
	$orderStr	= " ORDER BY isFeatured DESC, r_sortValue DESC";
	$oCoupons 	= $oCouponImpl->getCustoms($where, $orderStr, $page, 15);
	
	foreach ($oCoupons as $oCoupon) {
		$oCouponAttribute = $oCAttributeImpl->getLists($oCoupon->getId());
		$oCoupon->setAttributes($oCouponAttribute);
		
		$oMerchant 	= $oMerchantImpl->getById($oCoupon->getMerchantId());
		
		if($oCoupon->getCreativeType() == 1){
			$oImage = $oCouponImpl->getCouponImage($oCoupon->getId());
			
			if(is_object($oImage)){
				$oCoupon->setCouponImage($oImage);
			}else{
				$oCoupon->setCreativeType(2);
			}
		}
		
		if($oCoupon->getCreativeType() == 2){
			$oLogo = $oMerchantImpl->getLogo($oCoupon->getMerchantId());
			
			if(!is_object($oLogo)){
				$oLogo = new ImageDomain();
				
				$oLogo->setId(0);
				$oLogo->setExtName('gif');				
			}
			
			$oMerchant->setLogo($oLogo);
		}
		
		$oCoupon->setMerchant($oMerchant);
		$oCoupon->setCategory($oCategory);
	}
	
	$smarty->assign('oTags', $oTags);
	$smarty->assign('oCoupons', $oCoupons);
	$smarty->assign('oRecommendedCoupons', $oRecommendedCoupons);
	
	$smarty->assign('oCategory', $oCategory);
	$smarty->assign('oCatTree', $oCatTree);
	
	
	$smarty->assign('pageList', $oCouponImpl->getPageList(5));
	$smarty->assign('nextPage', $oCouponImpl->getNextPage());
	$smarty->assign('prePage', $oCouponImpl->getPrePage());
	$smarty->assign('pageCount', $oCouponImpl->getPageCount());
	$smarty->assign('page', $oCouponImpl->getPage());
	
	$smarty->assign('id', $id);
	$smarty->assign('order', $order);
}

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL_HTTP);
$smarty->assign('DEBUG', DEBUG);

$smarty->assign('isCategory', "yes");


$smarty->display('category.html', $cacheId, $cacheId);
?>
